<template>
  <el-aside width="200px" class="main-sidebar">
    <!-- 用户信息区域 -->
    <div class="user-profile">
      <el-avatar :size="64" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
      <div class="user-info">
        <div class="user-name">{{ userInfo.username }}</div>
        <div class="user-role">{{ userInfo.role === 'ADMIN' ? 'Admin' : 'User' }}</div>
      </div>
    </div>
    
    <!-- 主导航菜单 -->
    <el-menu
      :default-active="activeMenu"
      class="main-menu"
      @select="handleSelect"
      text-color="#333333"
      active-text-color="#2D5BFF"
      background-color="transparent">
      <el-menu-item index="dashboard">
        <!-- <el-icon><DataBoard /></el-icon> -->
        <span>My dashboard pages</span>
      </el-menu-item>

      <!-- 管理员可见的菜单 -->
      <template v-if="userInfo.role === 'ADMIN'">
        <el-menu-item index="create">
          <!-- <el-icon><Plus /></el-icon> -->
          <span>Create New</span>
        </el-menu-item>
        <el-menu-item index="permissions">
          <!-- <el-icon><Lock /></el-icon> -->
          <span>Modify Page Permissions</span>
        </el-menu-item>
        <el-menu-item index="sales">
          <!-- <el-icon><TrendCharts /></el-icon> -->
          <span>Edit dashboard pages</span>
        </el-menu-item>
      </template>
    </el-menu>

    <!-- 退出登录按钮 -->
    <div class="logout-section">
      <el-button type="text" @click="handleLogout" class="logout-btn">
        <el-icon><SwitchButton /></el-icon>
          Logout
      </el-button>
    </div>
  </el-aside>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  DataBoard,
  Plus,
  Lock,
  TrendCharts,
  SwitchButton
} from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const username = ref('User')
const activeMenu = ref('dashboard')
const userRole = ref('')

// 只负责切换子菜单显示
const emit = defineEmits(['menu-select'])
const handleSelect = (index) => {
  activeMenu.value = index
  
  emit('menu-select', index)
  
  // 添加路由导航逻辑
  if (index === 'create') {
    router.push('/create')
  } else if (index === 'dashboard') {
    router.push('/analysis/core')
  } else if (index === 'permissions') {
    router.push('/permissions/dashboard/1')
  } else if (index === 'sales') {
    // 导航到销售仪表板编辑页面
    router.push('/dashboard/sales')
  }
}

const router = useRouter()

const userInfo = ref({})

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    // 清除token和用户信息
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    
    // 跳转到登录页
    router.push('/login')
  } catch (error) {
    // 用户点击取消
  }
}

onMounted(() => {
  userInfo.value = JSON.parse(localStorage.getItem('userInfo') || '{}')
  userRole.value = userInfo.value.role
})
</script>

<style scoped>
.main-sidebar {
  background-color: #fff;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  width: 200px !important;
  border-right: 1px solid #eee;
  display: flex;
  flex-direction: column;
  z-index: 1000;
}

.user-profile {
  padding: 24px;
  text-align: center;
  border-bottom: 1px solid #eee;
}

.user-info {
  margin-top: 12px;
}

.user-name {
  font-size: 16px;
  font-weight: 500;
  color: #333333;
}

.user-role {
  font-size: 12px;
  color: #999999;
  margin-top: 4px;
}

.main-menu {
  flex: 1;
  border-right: none;
}

.logout-section {
  padding: 16px;
  border-top: 1px solid #eee;
}

.logout-btn {
  width: 100%;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

:deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
  padding: 0 20px !important;
}

:deep(.el-menu-item.is-active) {
  background-color: #ECF5FE !important;
  border-radius: 4px;
  margin: 0 10px;
  width: calc(100% - 20px);
}

:deep(.el-menu-item:hover) {
  background-color: #ECF5FE !important;
  border-radius: 4px;
  margin: 0 10px;
  width: calc(100% - 20px);
}
</style> 